//
//  WGBProgressView.m
//  WGBProgressView
//
//  Created by apple on 2018/4/10.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "WGBProgressView.h"
#import "UICountingLabel.h"


#define  kProgressBarHeight  5

@interface WGBProgressView()

@property (nonatomic,strong) UIView *progressBgView;
@property (nonatomic,strong) UIView *progressView;
@property (nonatomic,strong) UIView *dotView;
@property (nonatomic,strong)  CAShapeLayer *ringLayer;
@property (nonatomic,strong) UICountingLabel *displayLabel;

@end


@implementation WGBProgressView

- (UIView *)progressBgView{
    if (!_progressBgView) {
        _progressBgView = [[UIView alloc] initWithFrame: self.bounds];
        _progressBgView.width -= 44;
        _progressBgView.height = kProgressBarHeight;
        _progressBgView.y = (self.height - kProgressBarHeight)/2.0;
        [self addSubview:_progressBgView];
    }
    return _progressBgView;
}

- (UIView *)dotView{
    if (!_dotView) {
        CGFloat dotWH = kProgressBarHeight + 1 ;
        CGFloat dotCenterY = dotWH /2 ;
        _dotView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, dotWH , dotWH)];
        _dotView.backgroundColor = [UIColor redColor];
        _dotView.layer.cornerRadius = dotWH/2.0;
        _dotView.center = CGPointMake(dotCenterY, dotCenterY);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter: _dotView.center radius:(dotWH+6)/2.0 startAngle:0 endAngle:M_PI*2 clockwise:YES];
        self.ringLayer.path = path.CGPath;
        [_dotView.layer addSublayer: self.ringLayer];
        
        [self.progressBgView addSubview: _dotView];
    }
    return _dotView;
}

- (CAShapeLayer *)ringLayer{
    if (!_ringLayer) {
        _ringLayer = [CAShapeLayer layer];
        _ringLayer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.6].CGColor;
    }
    return _ringLayer;
}

- (UIView *)progressView{
    if (!_progressView) {
        _progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0 , kProgressBarHeight)];
        _progressView.layer.cornerRadius = kProgressBarHeight/2.0;
        _progressView.backgroundColor = [UIColor redColor];
        [self.progressBgView addSubview:_progressView];
    }
    return _progressView;
}

- (UILabel *)displayLabel{
    if (!_displayLabel) {
        _displayLabel = [[UICountingLabel alloc] init];
        _displayLabel.textColor = RGB(90, 90, 90);
        _displayLabel.font = KFontSize(11);
        _displayLabel.textAlignment = NSTextAlignmentCenter;
        _displayLabel.format = @"%.f%%";
        [self addSubview: _displayLabel];
    }
    return _displayLabel;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
        
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}


- (void)setup{
    self.progressBgView.backgroundColor = [UIColor grayColor];
    self.progressBgView.layer.cornerRadius = kProgressBarHeight/2.0;
    self.displayLabel.frame = CGRectMake(CGRectGetMaxX(self.progressBgView.frame), 0, 44 , self.height);
    self.isAnimation = NO;
    [self dotView];
    [self progressView];
    [self displayLabel];
}

- (void)setDotColor:(UIColor *)dotColor{
    _dotColor = dotColor;
    self.ringLayer.fillColor = [[dotColor colorWithAlphaComponent:0.6] CGColor];
}

- (void)setTrackTintColor:(UIColor *)trackTintColor{
    _trackTintColor = trackTintColor;
    self.progressBgView.backgroundColor = trackTintColor;
}

- (void)setProgressTintColor:(UIColor *)progressTintColor{
    _progressTintColor = progressTintColor;
    self.progressView.backgroundColor = progressTintColor;
}

- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    self.isAnimation = YES;
}

- (void)setIsAnimation:(BOOL)isAnimation{
    _isAnimation = isAnimation;
    if (isAnimation) {
        [self startAnimation];
    }else{
        [self.displayLabel countFrom:0 to:self.progress*100];
        self.progressView.width = self.progressBgView.width*self.progress;
        self.dotView.x = CGRectGetMaxX(self.progressView.frame);
        self.displayLabel.text = [NSString stringWithFormat:@"%.f%%",self.progress*100];
    }
}


#pragma mark- 开始动画
- (void)startAnimation{
    [UIView animateWithDuration: 1 animations:^{
        [self.displayLabel countFrom:0 to:self.progress*100];
        self.progressView.width = self.progressBgView.width*self.progress;
        self.dotView.x = CGRectGetMaxX(self.progressView.frame);
        self.displayLabel.text = [NSString stringWithFormat:@"%.f%%",self.progress*100];
    }];
}



@end
